package common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;



/**
 *
 *@author bianmiao
 *@date 2014-8-20
 */

public class JUtil {
   
	//根据yyyy-MM格式的日期数据，计算所在月的天数
	public static int days(String ym){
		
		Calendar rightNow = Calendar.getInstance(); 
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM");  
		try { 
		rightNow.setTime(simpleDate.parse(ym)); //要计算你想要的月份，改变这里即可
		} catch (ParseException e) {e.printStackTrace();}
		int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days;
		
	}
	//根据yyyy格式的日期数据，计算所在月的天数
	public static int daysByYr(String yr){
		
		Calendar rightNow = Calendar.getInstance(); 
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy");  
		try { 
		rightNow.setTime(simpleDate.parse(yr)); //要计算你想要的月份，改变这里即可
		} catch (ParseException e) {e.printStackTrace();}
		int days = rightNow.getActualMaximum(Calendar.DAY_OF_YEAR);
		return days;
		
	}
	
	public static Map<String, Object> getYesterday() { 
   	 Map<String, Object> yesterday=new HashMap<String, Object>();
   	 Calendar cal = Calendar.getInstance(); 
   	 SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd"); 
     cal.add(Calendar.DATE,-1);
   	 yesterday.put("bdat", simpleDate.format(cal.getTime()));
   	 cal.add(Calendar.DATE, 6); 
   	 yesterday.put("edat", simpleDate.format(cal.getTime()));
   	 return yesterday;
   }

	public static Map<String, Object> getWeek() { 
    	 Map<String, Object> week=new HashMap<String, Object>();
    	 Calendar cal = Calendar.getInstance(); 
    	 SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd"); 
    	 int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2; 
    	 cal.add(Calendar.DATE, -day_of_week); 
    	 week.put("bdat", simpleDate.format(cal.getTime()));
    	 cal.add(Calendar.DATE, 6); 
    	 week.put("edat", simpleDate.format(cal.getTime()));
    	 return week;
    }
	
	public static Map<String, Object> getMonth() { 
	   	 Map<String, Object> month=new HashMap<String, Object>();
	     //获取当前月第一天：
	   	 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
	     Calendar c = Calendar.getInstance();    
	     c.add(Calendar.MONTH, 0);
	     c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
	     month.put("bdat",format.format(c.getTime()));   
	    //获取当前月最后一天
	     Calendar ca = Calendar.getInstance();    
	     ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
	     month.put("edat",format.format(ca.getTime()));
	     return month;
   }
	
	public static Map<String, Object> getYear() { 
	   	 Map<String, Object> year=new HashMap<String, Object>();
	   	 SimpleDateFormat format = new SimpleDateFormat("yyyy"); 
	     Calendar c = Calendar.getInstance();     
	     System.err.println();
	     year.put("bdat",format.format(c.getTime())+"-01-01");
	     year.put("edat",format.format(c.getTime())+"-12-31");
	     return year;
	    
  }
	public static Map<String, Object> getTendays() { 
		 Map<String, Object> tendays=new HashMap<String, Object>();
	     //获取当前月第一天：
	   	 SimpleDateFormat format2 = new SimpleDateFormat("dd");
	   	 SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM");
	     Calendar c = Calendar.getInstance();    
	     c.add(Calendar.MONTH, 0);
	     c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));   
	     if(Integer.parseInt(format2.format(new Date()))>0&&Integer.parseInt(format2.format(new Date()))<=10){
	    	  tendays.put("bdat", format3.format(c.getTime())+"-01");
	    	  tendays.put("edat", format3.format(c.getTime())+"-10");
	     }else if(Integer.parseInt(format2.format(new Date()))>=11&&Integer.parseInt(format2.format(new Date()))<=20){
	    	  tendays.put("bdat", format3.format(c.getTime())+"-11");
	    	  tendays.put("edat", format3.format(c.getTime())+"-20");
	     }else if(Integer.parseInt(format2.format(new Date()))>20){
	    	  tendays.put("bdat", format3.format(c.getTime())+"-21");
	    	  tendays.put("edat", format3.format(c.getTime())+"-"+format2.format(c.getTime()));
	     }
	     return tendays;	    
 }
	public static void main(String[] args) {
		JUtil.getTendays();
	}
	/*
	 * 传入一个Map，把所有value为null的改为指定字符串
	 * @paras map：要处理的字符串
	 * @paras nullstr：value为空时修改的字符串
	 * @return 返回修改后的map
	 */
	public static Map<String,Object> MapValueNull(Map<String,Object> map,String nullstr){
		Set<String> set = map.keySet();
		for(Object o : set){
			if(map.get(o)==null){
				map.put((String) o, nullstr);
			}
		}
		return map;
	}

	/*
	 * 判断指定字符串是否为null
	 * @param o：传入一个对象
	 * @return 返回字符串
	 */
	public static String snull(Object o) {
		if (o == null || o.toString().trim().equals("") || o.toString().trim().equalsIgnoreCase("null") || o.toString().trim().equals("undefined")) {
			return null;
		} else {
			return o.toString();
		}
	}
	
	/*
	 * 传入用逗号分隔的字符串返回指定格式
	 * @param s：传入要处理的字符串
	 * @return 返回截取后的字符串
	 */
	public static String regValues(Object s) {
		if(snull(s)==null){return null;}
		String database=""; 
		String[] arr = s.toString().split(",");
		int len = arr.length;
		for(int i=0;i<len;i++){
			String tmp = arr[i].trim();
			if(!tmp.equals("")){
				database+="'"+tmp+"',";
			}
		}
		database = SubLastChar(database, ",");
		return database;
	}
	
	/*
	 * 去掉字符串最后的指定字符
	 * @param s：传入被截取的字符串
	 * @param c：截取的分割字符
	 * @return 返回截取后的字符串
	 */
	public static String SubLastChar(String s, String c) {
		if(s==null){return s;}
		if (s.lastIndexOf(c) != -1) {
			s = s.substring(0, s.lastIndexOf(c));
		}
		return s;
	}
	
	/*
	 * 通用参数处理工具方法
	 * @param map：原始map
	 * @param keyName：值设入map的key名称，不允许传入null或者空串
	 * @param value：参数值
	 * @return 在原始map中添加后的map
	 */
	public static Map<String, Object> para(Map<String, Object> map, String keyName , Object value) {
		if (keyName == null || keyName.trim().equals("")) {return map;}
		if(snull(value)!=null){
			map.put(keyName, value);
		}
		return map;
	}
	
	/*
	 * 分页参数的设置
	 * @param map：要植入的map
	 * @param para：参数map
	 * @return 返回处理后的map
	 */
	public static Map<String, Object> fenyeSet(Map<String, Object> map,Map<String, Object> para){
		int pageSize = 10;//页大小
		int currentPage = 1;//当前页
		if(para.get("pageSize")!=null){
			try{pageSize = Integer.parseInt(para.get("pageSize").toString());}catch (Exception e) {}
			if(pageSize<1){pageSize=1;}
		}
		if(para.get("currentPage")!=null){
			try{currentPage = Integer.parseInt(para.get("currentPage").toString());}catch (Exception e) {}
			if(currentPage<1){currentPage=1;}
		}
		map.put("pageSize", pageSize);
		map.put("currentPage", currentPage);
		return map;
	}
	
	/**
     * 当前季度的开始时间，即2012-01-1 00:00:00
     * 
     * @return
     */
    public static  String getCurrentQuarterStartTime() {
    	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = shortSdf.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度的结束时间，即2012-03-31 23:59:59
     * 
     * @return
     */
    public static  String getCurrentQuarterEndTime() {
    	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        String now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = shortSdf.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    /**
     * 当前季度
     * 
     * @return
     */
    public static  String getCurrentSeason() {
    	SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int currentYear = c.get(Calendar.YEAR);
    	try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                return currentYear+"年第1季度取水量预警信息";
            } else if (currentMonth >= 4 && currentMonth <= 6) {
            	return currentYear+"年第2季度取水量预警信息";
            } else if (currentMonth >= 7 && currentMonth <= 9) {
            	return currentYear+"年第3季度取水量预警信息";
            } else if (currentMonth >= 10 && currentMonth <= 12) {
            	return currentYear+"年第4季度取水量预警信息";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
